package com.webframe.web.controller;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.mlw.vlh.ValueList;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BasicDynaBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.webframe.util.StringUtil;
@Controller
@RequestMapping("/extTable")
public class ExtTableController extends BaseController
{
	@Autowired
	private ValueListController valueListController;
	@RequestMapping
	public String index(HttpServletRequest req, HttpServletResponse res) throws Exception
	{
		String key = req.getParameter("key"); // key
		ValueList list;
		try
		{
			list = valueListController.getValueList(key, req);
		} catch (Exception e)
		{
			String message = e.getMessage();
			if (message == null)
			{
				message = "java.lang.NullPointerException";
			}
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/plain");
			PrintWriter out = res.getWriter();
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			JSONObject root = new JSONObject();
			JSONObject msg = new JSONObject();
			JSONObject error = new JSONObject();
			JSONArray array = new JSONArray();
			array.add(msg);
			msg.put("信息", message);
			error.put("brief", message);
			error.put("detail", sw.toString());
			root.put("totalCount", 1);
			root.put("data", array);
			root.put("error", error);
			out.print(root.toString());
			out.flush();
			out.close();
			return null;
		}
		// 判断是否为下拉框
		String dropdownlist = req.getParameter("dropdownlist");
		if ("true".equalsIgnoreCase(dropdownlist))
		{
			String is_new_version_ = req.getParameter("is_new_version_"); // 判断是否封装版本
			if (is_new_version_ == null)
			{
				outWrite(res, valueListController.dropdownlist(list).toString());
			} else
			{
				outWrite(res, this.dropdownlist_new(list).toString());
			}
			return null;
		}
		// 拼装json串
		outWrite(res, valueListController.wrapJson(list));
		return null;
	}

	/**
	 * 下拉列表的情况,封装版本,带分页
	 * 
	 * @param sql 数据源
	 * @return json
	 */
	@SuppressWarnings("rawtypes")
	public StringBuffer dropdownlist_new(ValueList valueList)
	{
		StringBuffer sb = new StringBuffer();
		sb.append("{\"totalCount\":"
					+ valueList.getValueListInfo().getTotalNumberOfEntries()
					+ ",\"data\":[");
		List list = valueList.getList();
		BasicDynaBean dynaBean = null;
		for (Iterator iterator = list.iterator(); iterator.hasNext();)
		{
			dynaBean = (BasicDynaBean) iterator.next();
			sb.append("{name:\""
						+ dynaBean.get("name").toString().trim()
						+ "\",valtext:\""
						+ StringUtil.replaceSpecilCharForJson(dynaBean.get("valtext")
							.toString()
							.trim())
						+ "\"}");
			if (iterator.hasNext())
			{
				sb.append(",");
			}
		}
		sb.append("]}");
		return sb;
	}
}
